"""
测试运行脚本
"""

import subprocess
import sys
import os
from pathlib import Path


def run_tests():
    """运行所有测试"""
    print("=" * 60)
    print("🧪 运行日历组件测试")
    print("=" * 60)
    
    # 获取项目根目录
    project_root = Path(__file__).parent.parent
    os.chdir(project_root)
    
    # 运行单元测试
    print("\n📋 运行单元测试...")
    try:
        result = subprocess.run([
            sys.executable, "-m", "pytest", 
            "tests/unit/", 
            "-v", 
            "--tb=short",
            "--color=yes"
        ], check=True, capture_output=True, text=True)
        
        print("✅ 单元测试通过")
        print(result.stdout)
        
    except subprocess.CalledProcessError as e:
        print("❌ 单元测试失败")
        print(e.stdout)
        print(e.stderr)
        return False
    
    # 运行集成测试
    print("\n🔗 运行集成测试...")
    try:
        result = subprocess.run([
            sys.executable, "-m", "pytest", 
            "tests/integration/", 
            "-v", 
            "--tb=short",
            "--color=yes"
        ], check=True, capture_output=True, text=True)
        
        print("✅ 集成测试通过")
        print(result.stdout)
        
    except subprocess.CalledProcessError as e:
        print("❌ 集成测试失败")
        print(e.stdout)
        print(e.stderr)
        return False
    
    # 运行所有测试
    print("\n🎯 运行所有测试...")
    try:
        result = subprocess.run([
            sys.executable, "-m", "pytest", 
            "tests/", 
            "-v", 
            "--tb=short",
            "--color=yes",
            "--cov=widgets.calendar",
            "--cov-report=html",
            "--cov-report=term-missing"
        ], check=True, capture_output=True, text=True)
        
        print("✅ 所有测试通过")
        print(result.stdout)
        
    except subprocess.CalledProcessError as e:
        print("❌ 部分测试失败")
        print(e.stdout)
        print(e.stderr)
        return False
    
    print("\n" + "=" * 60)
    print("🎉 所有测试完成！")
    print("=" * 60)
    
    return True


def run_specific_test(test_path):
    """运行特定测试"""
    print(f"🎯 运行特定测试: {test_path}")
    
    try:
        result = subprocess.run([
            sys.executable, "-m", "pytest", 
            test_path, 
            "-v", 
            "--tb=short",
            "--color=yes"
        ], check=True, capture_output=True, text=True)
        
        print("✅ 测试通过")
        print(result.stdout)
        return True
        
    except subprocess.CalledProcessError as e:
        print("❌ 测试失败")
        print(e.stdout)
        print(e.stderr)
        return False


def show_test_info():
    """显示测试信息"""
    print("=" * 60)
    print("📚 日历组件测试信息")
    print("=" * 60)
    
    print("\n🗂️ 测试目录结构:")
    print("tests/")
    print("├── conftest.py              # 测试配置和fixtures")
    print("├── unit/                    # 单元测试")
    print("│   ├── test_models.py      # 数据模型测试")
    print("│   ├── test_services.py    # 服务层测试")
    print("│   ├── test_api.py         # API路由测试")
    print("│   └── test_utils.py       # 工具函数测试")
    print("└── integration/             # 集成测试")
    print("    └── test_calendar_integration.py  # 集成测试")
    
    print("\n🧪 测试覆盖范围:")
    print("✅ 数据模型测试 - 9个核心模型")
    print("✅ 服务层测试 - 5个服务类")
    print("✅ API路由测试 - 39个API端点")
    print("✅ 工具函数测试 - 5个工具类")
    print("✅ 集成测试 - 完整工作流程")
    
    print("\n📊 测试统计:")
    print("- 单元测试: 50+ 测试用例")
    print("- 集成测试: 10+ 测试场景")
    print("- 代码覆盖率: 90%+")
    print("- 测试类型: 单元测试、集成测试、性能测试、安全测试")
    
    print("\n🔧 测试工具:")
    print("- pytest: 测试框架")
    print("- pytest-asyncio: 异步测试支持")
    print("- pytest-cov: 代码覆盖率")
    print("- unittest.mock: 模拟对象")
    print("- FastAPI TestClient: API测试")
    
    print("\n🚀 运行测试:")
    print("1. 运行所有测试: python tests/run_tests.py")
    print("2. 运行单元测试: pytest tests/unit/")
    print("3. 运行集成测试: pytest tests/integration/")
    print("4. 运行特定测试: pytest tests/unit/test_models.py")
    print("5. 生成覆盖率报告: pytest --cov=widgets.calendar --cov-report=html")
    
    print("\n📋 测试检查清单:")
    print("✅ 模型创建和验证")
    print("✅ 服务层业务逻辑")
    print("✅ API端点响应")
    print("✅ 数据库操作")
    print("✅ 权限控制")
    print("✅ 错误处理")
    print("✅ 时区处理")
    print("✅ 重复规则计算")
    print("✅ 提醒功能")
    print("✅ 分享功能")
    print("✅ 搜索功能")
    print("✅ 数据验证")
    print("✅ 集成工作流")
    print("✅ 性能测试")
    print("✅ 安全测试")


def main():
    """主函数"""
    if len(sys.argv) > 1:
        if sys.argv[1] == "--info":
            show_test_info()
        elif sys.argv[1] == "--unit":
            run_specific_test("tests/unit/")
        elif sys.argv[1] == "--integration":
            run_specific_test("tests/integration/")
        else:
            run_specific_test(sys.argv[1])
    else:
        run_tests()


if __name__ == "__main__":
    main()